#include "DigitalCallOption.h"
#include "MonteCarloPricer.h"
#include "matlib.h"
#include "testing.h"


DigitalCallOption::DigitalCallOption() :
    strike(0.0),
    maturity(0.0) {
}

double DigitalCallOption::payoff(double stockAtMaturity) const {
    if (stockAtMaturity <= strike) {
        return 0.0;
    }
    else {
        return 1.0;
    }
}

double DigitalCallOption::getMaturity() const {
    return maturity;
}


//////////////////////////
//
//  Test the call option class
//
//
//////////////////////////

static void testDigitalCallOptionPrice() {
    rng("default");

    BlackScholesModel m;
    m.volatility = 0.1;
    m.riskFreeRate = 0.05;
    m.stockPrice = 100.0;
    m.drift = 0.1;

    DigitalCallOption dco;
    dco.strike = 1100000;
    dco.maturity = 2;

    DigitalCallOption dco1;
    dco1.strike = 0.1;
    dco1.maturity = dco.maturity;

    // our pricer can price puts and calls
    MonteCarloPricer pricer;
    double price0 = pricer.price(dco, m);
    ASSERT_APPROX_EQUAL(price0, 0.0, 0.1);
    double price1 = pricer.price(dco1, m);
    ASSERT_APPROX_EQUAL(price1, 1.0, 0.1);
}

static void testPayoff() {
    DigitalCallOption dco;
    dco.strike = 105.0;
    dco.maturity = 2.0;
    ASSERT_APPROX_EQUAL(dco.payoff(110.0), 1, 0.001);
    ASSERT_APPROX_EQUAL(dco.payoff(100.0), 0, 0.001);
}



void testDigitalCallOption() {
 
    TEST(testPayoff);
    TEST(testDigitalCallOptionPrice);
}